package com.faithbj.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.faithbj.common.dao.impl.BaseDaoImpl;
import com.faithbj.common.model.CheckTotal;
import com.faithbj.dao.CheckTotalDao;

@Repository("checkTotalDao")
public class CheckTotalDaoImpl extends BaseDaoImpl<CheckTotal, String> implements CheckTotalDao {

    /**
     * 根据查询条件查询核对总体情况
     */
    @Override
    public List<CheckTotal> queryCheckTotalListByCondition(Map condition) {
        // TODO Auto-generated method stub
        Session session = this.getSession();
        Criteria criteria = session.createCriteria(CheckTotal.class);
        if (condition.get("id") != null) {
            criteria.add(Restrictions.eq("id", condition.get("id")));
        }
        if (condition.get("regionId") != null) {
            criteria.add(Restrictions.eq("regionId", condition.get("regionId")));
        }
        if (condition.get("acceptStartTime") != null) {
            criteria.add(Restrictions.ge("handleTime", condition.get("acceptStartTime")));
        }
        if (condition.get("acceptEndTime") != null) {
            criteria.add(Restrictions.le("handleTime", condition.get("acceptEndTime")));
        }
        criteria.setProjection(null);
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    }

    @Override
    public List<CheckTotal> queryListForBatching(boolean batched, String[] acceptNums) {
        String hql = "select ct from CheckTotal as ct " + "where ct.batched=:batched";
        if (acceptNums != null && acceptNums.length != 0) {
            hql += " and ct.id in(:acceptNums)";
        }
        return getSession().createQuery(hql).setParameter("batched", batched)
                .setParameterList("acceptNums", acceptNums).list();
    }

    @Override
    public List<CheckTotal> queryListForBatching(boolean batched) {
        String hql = "select ct from CheckTotal as ct " + "where ct.batched=:batched";
        return getSession().createQuery(hql).setParameter("batched", batched).list();
    }

    @Override
    public List<CheckTotal> queryListByIds(List<String> checkTotalIds) {
        String hql = "select ct from CheckTotal as ct " + "where ct.id in(:checkTotalIds)";
        return getSession().createQuery(hql).setParameterList("checkTotalIds", checkTotalIds).list();
    }

    @Override
    public String queryMaxBatchNumByRegionId(String regionId) {
        String hql = "select max(ct.batchNum) from CheckTotal as ct " + "where ct.regionId=:regionId";
        return (String) getSession().createQuery(hql).setParameter("regionId", regionId).uniqueResult();
    }

    @Override
    public List<CheckTotal> getCheckTotalListForApplyInfoByPage(int from, int size) {
        Session session = this.getSession();
        return session.createCriteria(CheckTotal.class).setFirstResult(from).setFetchSize(size).list();
    }
}
